package com.shujujiegou.队列.java模拟双端队列.使用链表实现;

/**
 * 使用链表实现双向队列
 */
public class DoubleQueue {
    private Integer size;

    private DataNode head;

    private DataNode tail;

    public DoubleQueue() {
        this.size = 0;
        this.head = null;
        this.tail = null;
    }

    public void addFirst(Object object) {
        DataNode dataNode = new DataNode(null, this.head, object);
        if(isEmpty()) {
            this.tail = dataNode;
        } else {
            this.head.setFron(dataNode);
        }

        this.head = dataNode;
        this.size ++;
    }

    public void addLast(Object object) {
        DataNode dataNode = new DataNode(this.tail, null, object);
        if(!isEmpty()) {
            this.tail.setNext(dataNode);
        }

        this.tail = dataNode;
        this.size ++;
    }

    public Object removeFirst() {
        Object target = null;
        if(isEmpty()) {
            return target;
        } else {
            target = this.head.getObject();
            if(this.head.getNext() == null) {
                this.tail = null;
            }

            this.size --;
            return target;
        }
    }

    public Object removeLast() {
        Object target = null;
        if(isEmpty()) {
            return target;
        } else {
            target = this.tail.getObject();
            this.tail = this.tail.getFron();
            if(this.tail == null) {
                this.head = null;
            }
        }

        this.size --;
        return target;
    }

    public void clear() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    /**
     * 队首循环遍历
     */
    public void foreachFirst() {
        if(isEmpty()) {
            throw new RuntimeException("当前队列为空");
        } else {
            int temp = this.size;
            while (temp > 0) {
                System.out.println(this.head.getObject());
                this.addLast(removeFirst());
                temp--;
            }
            System.out.println("循环遍历完成！");
        }
    }

    /**
     * 队尾循环遍历
     */
    public void foreachLast() {
        if(isEmpty()) {
            throw new RuntimeException("当前队列为空");
        } else {
            int temp = this.size;
            while(temp > 0) {
                System.out.println(this.tail.getObject());
                this.addLast(removeLast());
                temp --;
            }
        }
    }

    private Boolean isEmpty() {
        return this.size <= 0 ? true : false;
    }

    /**
     * 链表节点
     */
    public class DataNode {
        private DataNode fron;
        private DataNode next;
        private Object object;

        public DataNode(DataNode fron, DataNode next, Object object) {
            this.fron = fron;
            this.next = next;
            this.object = object;
        }

        public DataNode getFron() {
            return fron;
        }

        public void setFron(DataNode fron) {
            this.fron = fron;
        }

        public DataNode getNext() {
            return next;
        }

        public void setNext(DataNode next) {
            this.next = next;
        }

        public Object getObject() {
            return object;
        }

        public void setObject(Object object) {
            this.object = object;
        }
    }
}
